package com.monkeygroup.mygoplus.view.home.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

//import com.facebook.drawee.view.SimpleDraweeView;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.LoopPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.http.PersonHttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;
import com.monkeygroup.mygoplus.view.home.activity.AbroadThingActivity;
import com.monkeygroup.mygoplus.view.home.activity.BuildCaseActivity;
import com.monkeygroup.mygoplus.view.home.activity.CommunityActivity;
import com.monkeygroup.mygoplus.view.home.activity.PubliseedChooseActivity;
import com.monkeygroup.mygoplus.view.home.activity.RentActivity;
import com.monkeygroup.mygoplus.view.home.activity.SellHouseActivity;
import com.monkeygroup.mygoplus.view.home.activity.TopActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/12.
 */
public class HomeFragment extends Fragment implements View.OnClickListener {
    private View mView;
    private final String TAG = "HomeFragment";
    private RollPagerView mViewPager;
    private Activity mParent = null;

    private PersonHttpModel httpLoginModel;
    private PersonHttpModel personHttpModel;
    //請求參數
    private Map<String, String> params;
    private PersonalNomalInfo user;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_home, container, false);
            mParent =getActivity();
            initView();
            initListener();
        }

        super.onCreate(savedInstanceState);
        mViewPager = (RollPagerView) mView.findViewById(R.id.home_view_pager);
        mViewPager.setAdapter(new ImageLoopAdapter(mViewPager));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mViewPager.setHintView(new ColorPointHintView(getContext(),Color.parseColor("#DC473A"),Color.parseColor("#88ffffff")));
        }
        mViewPager.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getActivity(),"图片 "+position+" clicked",Toast.LENGTH_SHORT).show();
            }
        });

        params = new HashMap<>();
        user = LocalInfo.getUserInfo(mParent);

        params.put("appacc",user.getMobile());

        initRequest();

        return mView;
    }

    private class ImageLoopAdapter extends LoopPagerAdapter {
        int[] imgs = new int[]{
                R.mipmap.home_rollerpage_one,
                R.mipmap.home_rollerpage_two,
                R.mipmap.home_rollerpage_three
        };
//        String[] imgs = new String[]{"/storage/emulated/0/DCIM/Camera/20161020_114453.jpg","/storage/emulated/0/DCIM/Camera/20161020_174720.jpg","/storage/emulated/0/DCIM/Camera/20161022_133645.jpg"};






        public ImageLoopAdapter(RollPagerView viewPager) {

            super(viewPager);
        }

        @Override
        public View getView(ViewGroup container, int position) {
//            SimpleDraweeView view = new SimpleDraweeView(container.getContext());
//            view.setScaleType(SimpleDraweeView.ScaleType.FIT_XY);
//            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//            view.setImageResource(imgs[position]);
//            return view;
            ImageView view = new ImageView(container.getContext());
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            view.setImageResource(imgs[position]);
//            view.setImageURI(Uri.parse("file:"+imgs[position].toString()));
            return view;
        }

        @Override
        public int getRealCount() {
            return imgs.length;
        }


    }

    private void initView() {


    }

    private void initListener() {
        mView.findViewById(R.id.layout_shortcut).setOnClickListener(this);
        mView.findViewById(R.id.btn_home_sell).setOnClickListener(this);
        mView.findViewById(R.id.btn_home_rent).setOnClickListener(this);
        mView.findViewById(R.id.btn_home_assignment).setOnClickListener(this);
        mView.findViewById(R.id.btn_home_newproject).setOnClickListener(this);
        mView.findViewById(R.id.layout_shortcut).setOnClickListener(this);
        mView.findViewById(R.id.btn_home_overseasproperty).setOnClickListener(this);
        mView.findViewById(R.id.btn_home_community).setOnClickListener(this);
        mView.findViewById(R.id.btn_home_publish).setOnClickListener(this);
        mView.findViewById(R.id.btn_home_serv).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_shortcut:
                break;
            case R.id.btn_home_sell:
                SellHouseActivity.gotoActivity(mParent);
                break;
            case R.id.btn_home_rent:
                RentActivity.gotoActivity(mParent);

                break;
            case R.id.btn_home_assignment:
                TopActivity.gotoActivity(mParent);
                break;
            case R.id.btn_home_newproject:
                BuildCaseActivity.gotoActivity(mParent);
                break;
            case R.id.btn_home_community:
                CommunityActivity.gotoActivity(mParent);
                break;
            case R.id.btn_home_publish:

                personHttpModel.requestUserInfo();
//                PubliseedChooseActivity.gotoActivity(mParent);
                break;
            case R.id.btn_home_overseasproperty:
                AbroadThingActivity.gotoActivity(mParent);
            default:
                break;
        }
    }

    private void initRequest(){

        personHttpModel = new PersonHttpModel(RequestUrl.isappaccbind,params,new PersonHttpModel.SimpleOnHttpRequestListener(){


            @Override
            public void onRequestFinished(String resultCode, Integer timestamp) {

            }

            @Override
            public void onRequestFinished(String resultCode, int seq, int isbind, String acc) {

                Log.e("resultCoderesultCode===",""+resultCode);
                //請求成功
                if("ERROR_OK".equals(resultCode)){
                    //已綁定
                    if(isbind==1){
                        user.setSeq(seq);
                        LocalInfo.saveUserInfo(mParent,user);
                        PubliseedChooseActivity.gotoActivity(mParent);
                    }
                    else{
                        Toast.makeText(mParent, "請綁定一個web賬號才能刊登物件",
                                Toast.LENGTH_SHORT).show();

                    }

                }
                else{
                    Toast.makeText(mParent, "請綁定一個web賬號才能刊登物件",
                            Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onRequestFinished(String resultCode, int seq, int flag, String flag_name, int secflag, String secflag_name) {

            }

        },true);



    }

}